/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RichTextEditor.java
 *
 * Created on May 19, 2010, 9:40:05 PM
 */
package codekeeper;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.lang.StringBuffer;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import org.jdesktop.application.Action;

/**
 *
 * @author tattooedpierre
 */
public class RichTextEditor extends javax.swing.JPanel {

    public StyledDocument doc;
    public SimpleAttributeSet simpleAttribs;
    public Style currentStyle;
    public JTextPane richTextPane;
    private int orderedListCount = 1;

    /** Creates new form RichTextEditor */
    public RichTextEditor() {
        initComponents();
        doc = jTextPaneMain.getStyledDocument();
        simpleAttribs = new SimpleAttributeSet();
        richTextPane = this.jTextPaneMain;

        btnGrpBulletStyle.setSelected(btnNormal.getModel(), Boolean.TRUE);
        richTextPane.addKeyListener(new KeyListener() {

            @Override
            public void keyTyped(KeyEvent e) {
                char key = e.getKeyChar();

                if (key == '\n') {
                    if (btnGrpBulletStyle.getSelection().equals(btnBullets.getModel())) {
                        SetBullet();
                    } else if (btnGrpBulletStyle.getSelection().equals(btnNumbered.getModel())){
                        SetNumbered();
                    }
                }
            }

            @Override
            public void keyPressed(KeyEvent e) {
            }

            @Override
            public void keyReleased(KeyEvent e) {
            }
        });
    }

    @Action
    public void SetBold() {
        if (btnBold.isSelected()) {
            simpleAttribs.removeAttribute(StyleConstants.CharacterConstants.Bold);
            btnBold.setSelected(Boolean.FALSE);
        } else {
            simpleAttribs.addAttribute(StyleConstants.CharacterConstants.Bold, Boolean.TRUE);
            btnBold.setSelected(Boolean.TRUE);
        }

        jTextPaneMain.setCharacterAttributes(simpleAttribs, true);
    }

    @Action
    public void SetItalics() {
        if (btnItalic.isSelected()) {
            simpleAttribs.removeAttribute(StyleConstants.CharacterConstants.Italic);
            btnItalic.setSelected(Boolean.FALSE);
        } else {
            simpleAttribs.addAttribute(StyleConstants.CharacterConstants.Italic, Boolean.TRUE);
            btnItalic.setSelected(Boolean.TRUE);
        }

        jTextPaneMain.setCharacterAttributes(simpleAttribs, true);
    }

    @Action
    public void SetUnderline() {
        if (btnUnderline.isSelected()) {
            simpleAttribs.removeAttribute(StyleConstants.CharacterConstants.Underline);
            btnUnderline.setSelected(Boolean.FALSE);
        } else {
            simpleAttribs.addAttribute(StyleConstants.CharacterConstants.Underline, Boolean.TRUE);
            btnUnderline.setSelected(Boolean.TRUE);
        }

        jTextPaneMain.setCharacterAttributes(simpleAttribs, true);
    }

    public void SetText(String text) throws BadLocationException {
        richTextPane.setText(text);
    }

    public void ClearText() throws BadLocationException {
        richTextPane.setText("");
    }

    @Action
    public void SetNormal() {
        btnGrpBulletStyle.setSelected(btnNormal.getModel(), Boolean.TRUE);
        orderedListCount = 1;
    }

    @Action
    public void SetBullet() {
        richTextPane.setText(new StringBuffer(richTextPane.getText()).insert(richTextPane.getSelectionEnd(), "     \u2022 ").toString());
        btnGrpBulletStyle.setSelected(btnBullets.getModel(), Boolean.TRUE);
        orderedListCount = 1;

    }

    @Action
    public void SetNumbered() {
        richTextPane.setText(new StringBuffer(richTextPane.getText())
                .insert(richTextPane.getSelectionEnd(), "     " + orderedListCount + ". ").toString());
        btnGrpBulletStyle.setSelected(btnNumbered.getModel(), Boolean.TRUE);
        orderedListCount++;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnGrpBulletStyle = new javax.swing.ButtonGroup();
        jToolBarMain = new javax.swing.JToolBar();
        btnBold = new javax.swing.JButton();
        btnItalic = new javax.swing.JButton();
        btnUnderline = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        btnNormal = new javax.swing.JButton();
        btnBullets = new javax.swing.JButton();
        btnNumbered = new javax.swing.JButton();
        jScrollPaneText = new javax.swing.JScrollPane();
        jTextPaneMain = new javax.swing.JTextPane();

        setName("Form"); // NOI18N
        setLayout(new java.awt.BorderLayout());

        jToolBarMain.setRollover(true);
        jToolBarMain.setName("jToolBarMain"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(codekeeper.CodeKeeperApp.class).getContext().getActionMap(RichTextEditor.class, this);
        btnBold.setAction(actionMap.get("SetBold")); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(codekeeper.CodeKeeperApp.class).getContext().getResourceMap(RichTextEditor.class);
        btnBold.setText(resourceMap.getString("btnBold.text")); // NOI18N
        btnBold.setFocusable(false);
        btnBold.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnBold.setName("btnBold"); // NOI18N
        btnBold.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBarMain.add(btnBold);

        btnItalic.setAction(actionMap.get("SetItalics")); // NOI18N
        btnItalic.setText(resourceMap.getString("btnItalic.text")); // NOI18N
        btnItalic.setFocusable(false);
        btnItalic.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnItalic.setName("btnItalic"); // NOI18N
        btnItalic.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBarMain.add(btnItalic);

        btnUnderline.setAction(actionMap.get("SetUnderline")); // NOI18N
        btnUnderline.setText(resourceMap.getString("btnUnderline.text")); // NOI18N
        btnUnderline.setFocusable(false);
        btnUnderline.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnUnderline.setName("btnUnderline"); // NOI18N
        btnUnderline.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBarMain.add(btnUnderline);

        jSeparator1.setName("jSeparator1"); // NOI18N
        jToolBarMain.add(jSeparator1);

        btnNormal.setAction(actionMap.get("SetNormal")); // NOI18N
        btnNormal.setText(resourceMap.getString("btnNormal.text")); // NOI18N
        btnGrpBulletStyle.add(btnNormal);
        btnNormal.setFocusable(false);
        btnNormal.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnNormal.setName("btnNormal"); // NOI18N
        btnNormal.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBarMain.add(btnNormal);

        btnBullets.setAction(actionMap.get("SetBullet")); // NOI18N
        btnBullets.setText(resourceMap.getString("btnBullets.text")); // NOI18N
        btnGrpBulletStyle.add(btnBullets);
        btnBullets.setFocusable(false);
        btnBullets.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnBullets.setName("btnBullets"); // NOI18N
        btnBullets.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBarMain.add(btnBullets);

        btnNumbered.setAction(actionMap.get("SetNumbered")); // NOI18N
        btnNumbered.setText(resourceMap.getString("btnNumbered.text")); // NOI18N
        btnGrpBulletStyle.add(btnNumbered);
        btnNumbered.setFocusable(false);
        btnNumbered.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnNumbered.setName("btnNumbered"); // NOI18N
        btnNumbered.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBarMain.add(btnNumbered);

        add(jToolBarMain, java.awt.BorderLayout.PAGE_START);

        jScrollPaneText.setName("jScrollPaneText"); // NOI18N

        jTextPaneMain.setName("jTextPaneMain"); // NOI18N
        jScrollPaneText.setViewportView(jTextPaneMain);

        add(jScrollPaneText, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBold;
    private javax.swing.JButton btnBullets;
    private javax.swing.ButtonGroup btnGrpBulletStyle;
    private javax.swing.JButton btnItalic;
    private javax.swing.JButton btnNormal;
    private javax.swing.JButton btnNumbered;
    private javax.swing.JButton btnUnderline;
    private javax.swing.JScrollPane jScrollPaneText;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JTextPane jTextPaneMain;
    private javax.swing.JToolBar jToolBarMain;
    // End of variables declaration//GEN-END:variables
}
